// pages/mine/mine.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login: false,
    showApp: false,
    serviceShow: false,
    loginShow:false,
    phone:false,
    phoneshow:false,
    window : false //控制app弹窗
  },
  handleCloseApp(){
    this.toggle('window', false)
  },
  login() {
    if(this.data.login){
      this.toggle('phoneshow', true)
      this.selectComponent('#phone').showClose()
    }else{
      this.toggle('loginShow', true)
      this.selectComponent('#login').showClose()
    }
  },
  // loginClose() {
  //   this.toggle('loginShow', false)
  // },
  toggleToast(e) {
    // console.log(toggleToast,e.detail)
    if (!e.detail.loginShow){
      this.toggle('loginShow', false)
    }
    this.getUserInfo()
  },
  toggleToast1(e) {
    console.log(toggleToast1,e.detail)
    if (!e.detail.phoneshow) {
      this.toggle('phoneshow', false)
    }
    this.getUserInfo()
  },
  serviceShow() {
    this.toggle('serviceShow', true)
  },
  serviceClose() {
    this.toggle('serviceShow', false)
  },
  check: function() {
    this.toggle("serviceShow", false);
    this.toggle("showApp", true);
  },
  onClose: function() {
    this.toggle("showApp", false);
  },
  contact: function(e) {
    console.log(e);
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    var that = this
    var dataset = e.currentTarget.dataset;
    var url = dataset.turn
    if (url =='/pages/myCardNew/myCardNew'){
      getApp().mtj.trackEvent('yqlw_business_card');
    }
    if (dataset.certification != undefined) {
      that.toggle("window", true);
      // url = url + "?certification=" + dataset.certification
      return false;
    }
    wx.navigateTo({
      url: url
    })
  },
  userDatail: function() {
    var that = this;
    app.req({
      url: "orgmini/user/mine",
      success: function(res) {
        if (200 == res.code) {
          if (res.data){
            that.toggle("userData", res.data);
          }else{
            that.toggle("userData", {});
          }
        }
      }
    });
  },
  getUserProfile(e) {
    wx.getUserProfile({
      lang:'zh_CN',
      desc:'用于完善会员资料',
      success: (res) => {
        console.log(res)
      },
      fail:error =>{
        console.log(error)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    that.toggle("window", false);
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }
    that.getUserInfo()
    wx.setNavigationBarTitle({
      title: '我的',
    })
  },
  getUserInfo() {
    var that = this
    wx.getSetting({
      success(res) {
        console.log(res)
        // if (!res.authSetting['scope.userInfo'] || wx.getStorageSync('uid') == '') {
        if ( wx.getStorageSync('uid') == '') {
          that.toggle("login", false);
        } else {
          that.toggle("login", true);
          var phone = wx.getStorageSync("phone")
          if (phone == '') {
            that.toggle("phone", true);
          }else{
            that.toggle("phoneshow", false);
            that.toggle("loginShow", false);
            that.toggle("phone", false);
          }
          app.getUserInfo(function(userInfo) {
            that.toggle("userInfo", userInfo);
            that.userDatail();
          }, true)
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getUserInfo();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '您有N位共同好友在××组织中，快去看看吧！',
      path: '/pages/newIndex/newIndex',
    }
  }
})